package com.igame.maingame;

import java.util.Map;
import java.util.TreeMap;

import com.jme.math.Vector3f;
import com.jme.renderer.ColorRGBA;
import com.jme.renderer.Renderer;
import com.jme.scene.Node;
import com.jme.scene.Spatial;
import com.jme.scene.Text;
import com.jme.scene.Spatial.CullHint;
import com.jme.scene.shape.Quad;
import com.jme.scene.state.BlendState;
import com.jme.scene.state.BlendState.DestinationFunction;
import com.jme.scene.state.BlendState.SourceFunction;
import com.jme.system.DisplaySystem;
import com.jme.util.stat.graph.DefColorFadeController;

public class ScreenTextManager {
	Node graphNode;
	Node allparent;
	int textid = 0;
	
	public ScreenTextManager(Node the_graph_node){
		graphNode = the_graph_node;
//		allparent = graphNode;
		allparent = new Node("this_graph");
		allparent.setCullHint(CullHint.Never);
		allparent.setRenderQueueMode(Renderer.QUEUE_ORTHO);
		graphNode.attachChild(allparent);
	}
	
	Map<String, ScreenTextNode> textNodeList = new TreeMap<String, ScreenTextNode>();
	
	public ScreenTextNode createTextNode(){
		ScreenTextNode node = new ScreenTextNode(String.format("textnode_%d", textid++));
		node.setCullHint(CullHint.Never);
		node.setRenderQueueMode(Renderer.QUEUE_ORTHO);
		allparent.attachChild(node);
		textNodeList.put(node.getName(), node);
		return node;
	}
	
	public ScreenText createText(Node textnode,String content){
		int nchild = 0;
		if(textnode.getChildren() != null)
			nchild = textnode.getChildren().size();
		ScreenText tx = new ScreenText(String.format("screen_text_%d", nchild), content);
		tx.setRenderState(Text.getDefaultFontTextureState());
		tx.setRenderState(Text.getFontBlend());
		//tx.setTextColor(ColorRGBA.blue);
		tx.setToCenter();
		textnode.attachChild(tx);
		updateScene();
		return tx;
	}
	
	public void updateScene(){
		graphNode.updateGeometricState(0, true);
		graphNode.updateRenderState();
	}
	
	private static BlendState hidestate;
	private static BlendState showstate;
	private static BlendState blockstate;
	static{
		hidestate = DisplaySystem.getDisplaySystem().getRenderer().createBlendState();
		hidestate.setBlendEnabled(true);
		hidestate.setSourceFunction(SourceFunction.Zero);
		hidestate.setDestinationFunction(DestinationFunction.One);
		
		showstate = DisplaySystem.getDisplaySystem().getRenderer().createBlendState();
		showstate.setBlendEnabled(true);
		showstate.setSourceFunction(SourceFunction.SourceAlpha);
		showstate.setDestinationFunction(DestinationFunction.OneMinusSourceAlpha);
		
		blockstate = DisplaySystem.getDisplaySystem().getRenderer().createBlendState();
		blockstate.setBlendEnabled(true);
		blockstate.setSourceFunction(SourceFunction.One);
		blockstate.setDestinationFunction(DestinationFunction.Zero);
	};
	
	public class ScreenTextNode extends Node{
		Quad blockplane;
		float changerate = 0.4f;
		float alphablock = 0.8f;
		public ScreenTextNode(String name){
			super(name);
			int width = DisplaySystem.getDisplaySystem().getWidth();
			int height = DisplaySystem.getDisplaySystem().getHeight();
			blockplane = new Quad(name,width,height);
			blockplane.setLocalTranslation(new Vector3f(width/2,height/2,0));
			blockplane.setCullHint(CullHint.Always);
			blockplane.getDefaultColor().set(ColorRGBA.black);
			blockplane.getDefaultColor().a = 0f;
			blockplane.setRenderState(showstate);
			blockplane.updateRenderState();
			this.attachChild(blockplane);
		}
		public void show(){
			this.setRenderState(showstate);
			blockplane.clearControllers();
			blockplane.addController(new DefColorFadeController(blockplane, 0f, changerate));
			for(Spatial sp:this.getChildren()){
				if(sp != blockplane){
					sp.setRenderState(showstate);
				}
			}
			this.updateRenderState();
		}
		public void hide(){
			this.setRenderState(hidestate);
			blockplane.clearControllers();
			blockplane.addController(new DefColorFadeController(blockplane, 0f, changerate));
			for(Spatial sp:this.getChildren()){
				if(sp != blockplane){
					sp.setRenderState(hidestate);
				}
			}
			this.updateRenderState();
		}
		public void block(){
			this.setRenderState(showstate);
			for(Spatial sp:this.getChildren()){
				if(sp != blockplane){
					sp.setRenderState(showstate);
				}
			}
			blockplane.clearControllers();
			blockplane.addController(new DefColorFadeController(blockplane, alphablock, changerate));
			this.updateRenderState();
		}
	}
	
	public class ScreenText extends Text{
		public void setString(String newstr){
			getText().replace(0, getText().length(), newstr);
		}
		public String getString(){
			return getText().toString();
		}
		public void show(){
			this.setRenderState(showstate);
			updateScene();
		}
		public void hide(){
			this.setRenderState(hidestate);
			updateScene();
		}
		public ScreenText(String name, String text){
			super(name,text);
		}
		public void setToCenter(){
			int width = DisplaySystem.getDisplaySystem().getWidth();
			int height = DisplaySystem.getDisplaySystem().getHeight();
			this.setLocalTranslation(new Vector3f(width/2-this.getWidth()/2,height/2-this.getHeight()/2,0));
		}
	}
}
